#!/bin/bash

# 检查是否为root用户
if [ "$EUID" -ne 0 ]; then
  echo "请使用root用户运行此脚本或使用sudo"
  exit 1
fi

# 检测操作系统类型
if [ -f /etc/os-release ]; then
  . /etc/os-release
  OS=$NAME
elif type lsb_release >/dev/null 2>&1; then
  OS=$(lsb_release -si)
else
  echo "无法检测操作系统"
  exit 1
fi

# 安装docker-compose
install_docker_compose() {
  local COMPOSE_VERSION="1.29.2"  # 可以根据需要更新版本号
  local ARCH=$(uname -m)
  local DOWNLOAD_URL="https://github.com/docker/compose/releases/download/${COMPOSE_VERSION}/docker-compose-$(uname -s)-${ARCH}"

  # 下载docker-compose
  curl -L "${DOWNLOAD_URL}" -o /usr/local/bin/docker-compose

  # 设置权限
  chmod +x /usr/local/bin/docker-compose

  # 创建符号链接
  ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

  # 验证安装
  docker-compose --version
}

# 根据操作系统执行安装
case $OS in
  "Ubuntu"|"Debian GNU/Linux"|"CentOS Linux"|"Red Hat Enterprise Linux")
    install_docker_compose
    ;;
  *)
    echo "此脚本不支持的操作系统: $OS"
    exit 1
    ;;
esac

echo "docker-compose 安装完成"